package com.example.mvvm.ui.my

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.mvvm.R
import com.example.mvvm.data.model.place.Province
import com.example.mvvm.databinding.ItemproBinding

//class MyComicAdapter(
//    context: Context?,
//    var resource: Int,
//    var objects: MutableList<String>
//) : ArrayAdapter<String>(context!!, resource, objects) {
//    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
//        val binding: ItemproBinding?
//        val view = if (convertView == null) {
//            /*
//            val v = LayoutInflater.from(context).inflate(resource, parent, false)
//            binding = DataBindingUtil.bind(v)
//             */
//
//            /*
//            DataBindingUtil.inflate(LayoutInflater.from(context), resource, parent, false)
//            这种方法返回的是binding.root
//             */
//            binding = DataBindingUtil.inflate(LayoutInflater.from(context), resource, parent, false)
//            binding.root.tag = binding
//            binding.root
//        } else {
//            binding = convertView.tag as ItemproBinding
//            convertView
//        }
//        binding?.data = objects[position]
//        binding?.llItem?.setOnClickListener {
//            Log.e("wode",objects[position])
////            Log.e("wode",objects[position].id)
//        }
//        return view
//
//    }
//}

class MyComicAdapter(
    var context: Context?,
    var list: MutableList<Province>
) : RecyclerView.Adapter<MyComicAdapter.MyViewHolder>() {


    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val binding: ItemproBinding? = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.itempro, parent, false)
        var holder: MyViewHolder
        binding?.let {
            holder = MyViewHolder(it)
            return holder
        }?:let{
            holder = onCreateViewHolder(parent, viewType)
            return holder
        }
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.binding.tvName.text= list[position]?.provinceName
        Log.e("Wodegg",list[position]?.provinceName)
        //立即执行绑定，在对view变化时效敏感的地方常用，不加这句有可能出现itemView更新滞后、闪烁等问题
        holder.binding.executePendingBindings()
    }

    override fun getItemCount(): Int {
        return if (list.isNullOrEmpty()) 0 else list!!.size
    }

//    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
//        lateinit var binding: ItemproBinding
//    }

    class MyViewHolder internal constructor(var binding: ItemproBinding) :
        RecyclerView.ViewHolder(binding.root)
}